package com.ncu.sprotapp.Controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.ncu.sprotapp.Mapper.UserMapper;
import com.ncu.sprotapp.Entity.userEntity;
import com.ncu.sprotapp.utils.JWTUTIl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ncu.sprotapp.vo.JsonResult;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/front/user")
public class userController {


    @Autowired
    private UserMapper userMapper;
    @GetMapping("/all")
    public JsonResult getList(){
        List<userEntity> users = userMapper.getAll();
        return JsonResult.success(users);
    }

    @PostMapping("/reg")
    public JsonResult reg(@RequestBody userEntity user){
        System.out.println(user);
        userEntity oneByName = userMapper.getOneByName(user.getUserName());

        if(oneByName != null){
            return JsonResult.fail("用户名重复");

        }
        boolean add = userMapper.add(user);
        if(add){
            return JsonResult.success(null);
        }else {
            return JsonResult.fail("注册失败");
        }


    }

    @PostMapping("/login")
    public JsonResult login(@RequestBody userEntity user){

        userEntity oneByName = userMapper.login(user);
        if(oneByName == null){
            return JsonResult.fail("用户名或者密码不正确");
        }
        return JsonResult.success("ok");
    }
    @PostMapping("/edit")
    public JsonResult edit(@RequestBody userEntity user){



        System.out.println(user);
        boolean update = userMapper.update(user);
        if(update){
            return JsonResult.success(null);
        }
        return JsonResult.fail("修改失败");
    }
}
